///<reference path="../../service/weixin.d.ts"/>
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import {GetVideoDetailService} from "../../service/http-service/getVideoDetail.service";
import {ApplicantService} from "../../service/http-service/applicant.service";
import {PayService} from "../../service/http-service/pay.service"

/**
 * Generated class for the PayPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage(
  {
    name: 'PayPage',
    segment: 'pay/:Id'

  }
)

@Component({
  selector: 'page-pay',
  templateUrl: 'pay.html',
})
export class PayPage {
  video = {"duration": ""};
  // video={"id": "", "userid": "", "username": "", "commidityId": "","comment":""};
  videoID: string;
  appIdWx: string;
  timeStampWx: string;
  nonceStrWx: string;
  packageWx: string;
  signTypeWx: string;
  paySignWx: string;
  orderNum: string;


  constructor(public payService: PayService,public applicationService: ApplicantService, public navCtrl: NavController, public navParams: NavParams, public getVideoDetailservice: GetVideoDetailService) {
    this.videoID = navParams.get("Id");
    // this.videoUrl = navParams.get("Url")
    // console.log("dddddddddddddd");
    // console.log(this.videoID);
    this.orderNum = navParams.get("orderNum")
    console.log("orderNum");
    console.log(this.orderNum)

  }

  ionViewWillEnter() {

    console.log('ionViewDidLoad PayPage');

    this.getVideoDetail();


  }

  getVideoDetail() {
    this.getVideoDetailservice.getVideoDetail(this.videoID).subscribe(resp => {
      if (resp.data !== null && resp.code == 200) {
        this.video = resp.data;


        // console.log(resp.data);
        // localStorage.setItem("video111", resp.data);
        // console.log(this.video.url)


      } else if (resp.data !== null && resp.code == 1401) {
        console.log("errrovideo");
        this.applicationService.freshTokenn();
      }
    }, error2 => {

      console.log(error2);
    });
  }


  // payDomain() {
  //   WeixinJSBridge.invoke(
  //     'getBrandWCPayRequest', {
  //       "appId": this.appIdWx,     //公众号名称，由商户传入
  //       "timeStamp": this.timeStampWx,         //时间戳，自1970年以来的秒数
  //       "nonceStr": this.nonceStrWx, //随机串
  //       "package": this.packageWx,
  //       "signType": this.signTypeWx,         //微信签名方式：
  //       "paySign": this.paySignWx //微信签名
  //     },
  //     function (res) {
  //       if (res.err_msg == "get_brand_wcpay_request:ok") {
  //         this.navCtrl.pop();
  //       }     // 使用以上方式判断前端返回,微信团队郑重提示：res.err_msg将在用户支付成功后返回    ok，但并不保证它绝对可靠。
  //     }
  //   );
  // }

  getnotPaydata() {


    console.log("11111111");
    this.getVideoDetailservice.getNotpayOrder(this.orderNum).subscribe(resp => {
      if (resp.data !== null && resp.code == 200) {
        console.log(resp.data);
        this.timeStampWx = resp.data.timeStamp;

        this.appIdWx = resp.data.appId;
        this.nonceStrWx = resp.data.nonceStr;
        this.paySignWx = resp.data.paySign;
        this.packageWx = resp.data.pkg;
        console.log("wxasdlfjalsdjkla");
        console.log(this.packageWx);


        this.signTypeWx = resp.data.signType;
        this.payService.payDomain(this.appIdWx,this.timeStampWx,this.nonceStrWx,this.packageWx,this.signTypeWx,this.paySignWx);

        // WeixinJSBridge.invoke(
        //   'getBrandWCPayRequest', {
        //     "appId": this.appIdWx,     //公众号名称，由商户传入
        //     "timeStamp": this.timeStampWx,         //时间戳，自1970年以来的秒数
        //     "nonceStr": this.nonceStrWx, //随机串
        //     "package": this.packageWx,
        //     "signType": this.signTypeWx, //微信签名方式：
        //     "paySign": this.paySignWx //微信签名
        //   },
        //   function (res) {
        //     if (res.err_msg =="get_brand_wcpay_request:ok")
        //     {
        //       window.history.go(-1);
        //       //  alert("支付成功err_code=" + res.err_code + ",err_desc=" + res.err_desc + ",err_msg=" + res.err_msg);
        //       // WeixinJSBridge.call('closeWindow');
        //
        //     }
        //     else if (res.err_msg == "get_brand_wcpay_request:cancel") {
        //       //  alert("用户取消err_code=" + res.err_code + ",err_desc=" + res.err_desc + ",err_msg=" + res.err_msg);
        //     }
        //     else {
        //       alert("  支付失败err_code=" + res.err_code + ",err_desc=" + res.err_desc + ",err_msg=" + res.err_msg);
        //     }
        //     // if (res.err_msg == "get_brand_wcpay_request:ok") {
        //     //   this.navCtrl.pop();
        //     //   window.history.back();
        //     // }     // 使用以上方式判断前端返回,微信团队郑重提示：res.err_msg将在用户支付成功后返回    ok，但并不保证它绝对可靠。
        //   }
        // );




        // this.navCtrl.pop();
        // window.history.go(-1);

        // let thumbNum=document.getElementById("myDiv");
        // thumbNum.querySelector('thumb_num').innerHTML='hjh';
      } else {

        alert(resp.msg)
      }
    }, error2 => {
      alert("请检查网络连接后重试")
      console.log("error22");
    })



  }


  getPaydata() {
    // history.pushState(null,null,"pay/pay");
    if (this.videoID == null || this.videoID == undefined || this.videoID == "") {
      alert("请刷新重试");
      return;
    }

    // console.log("11111111");

    if(this.orderNum==null|| this.orderNum == undefined || this.orderNum == ""){

    this.getVideoDetailservice.getOrder(this.videoID).subscribe(resp => {
      if (resp.data !== null && resp.code == 200) {
        console.log(resp.data);
        this.timeStampWx = resp.data.timeStamp;

        this.appIdWx = resp.data.appId;
        this.nonceStrWx = resp.data.nonceStr;
        this.paySignWx = resp.data.paySign;
        this.packageWx = resp.data.pkg;
        console.log("wxasdlfjalsdjkla");
        console.log(this.packageWx);


        this.signTypeWx = resp.data.signType;
        this.payService.payDomain(this.appIdWx,this.timeStampWx,this.nonceStrWx,this.packageWx,this.signTypeWx,this.paySignWx);

      } else {

        alert(resp.msg)
      }
    }, error2 => {
      alert("请检查网络连接后重试")
      console.log("error22");
    })
    }
    else{
      this.getnotPaydata();


    }
  }


}
